Introduction to Machine Learning

Appendix: Hypothesis Testing Fundamentals

Overview

This appendix introduces the basic idea of statistical hypothesis testing. It is a prerequisite for understanding the χ2 test and ANOVA F-test used in Chapter 06: Feature Selection. The goal here is not to teach statistics in detail. The goal is to understand what a statistical test is asking, what a p-value means, and how to interpret the final decision.

Central idea
We start with a default assumption, measure how unusual our observed data would be under that assumption, and use that evidence to decide whether the assumption should be rejected.

Learning Objectives

1. Why do we need hypothesis testing?

Machine learning often works with a dataset that contains a sample of observations rather than every possible observation in the population. Because a sample is only one random selection, its numbers will naturally change from sample to sample.

Interactive: sampling variation

Imagine a fair coin. Under the assumption that the coin is fair, the probability of heads is 50%. Does every sample of 20 flips contain exactly 10 heads? No. Try generating samples and notice how much the observed percentage moves around even when the underlying probability stays at 50%.

Single sample

Current sampleHeadsObserved percentageExpected under H0
———50%

100 samples (distribution)

Each bar is one simulated sample; its height is the percentage of heads in that sample. The dashed red line marks 50% — the value expected under H0.

This is the basic difficulty of statistical inference: an observed difference does not automatically mean that the underlying population is different. Some difference can appear simply because of sampling variability.

2. Population, sample, and the null hypothesis

Suppose we want to know whether two groups have different population means. We usually cannot measure the entire population, so we collect samples from the two groups and compare them.

TermBasic meaningExample
PopulationThe complete group we are interested in.All customers of a bank.
SampleThe observations actually collected.500 sampled customers.
ParameterA numerical property of the population.True population mean.
StatisticA numerical property calculated from the sample.Sample mean.

Null hypothesis: H0

The null hypothesis is the default assumption that we test against. In many introductory tests it represents no association, no difference, or no effect.

Examples

Alternative hypothesis: H1

The alternative hypothesis describes what we would consider evidence for instead of H0.

Important wording
A statistical test normally does not start by trying to prove H1. It asks whether the observed data provide sufficiently strong evidence to reject H0.

3. The logic of a statistical test

Most hypothesis tests can be understood using the following five-step sequence:

  1. State H0 and H1.
  2. Collect the sample data.
  3. Calculate a test statistic.
  4. Ask how unusual that statistic would be if H0 were true.
  5. Use the resulting p-value and a chosen significance level to make a decision.
One sentence to remember
A hypothesis test asks: “If H0 were really true, would my observed result be unusually extreme?”

4. What is a test statistic?

A test statistic is a number calculated from the sample that summarizes how far the observed data depart from what H0 predicts.

Different tests use different statistics. For example:

TestTest statisticBasic question
Chi-square\(\chi^2\)How far are observed counts from the counts expected under independence?
ANOVA\(F\)How large is between-group variation relative to within-group variation?

The formula changes from one test to another, but the purpose is the same: turn the observed data into a number that can be compared with what is expected under H0.

5. The key idea: what would happen if H0 were true?

This is the step students often skip mentally. We imagine that H0 is true, and then ask what values of the test statistic would commonly occur through random sampling.

Most results will be in the middle of the distribution. More extreme results occur less often.

Interactive: move the observed result

The curve below represents the distribution of a generic standardized test statistic when H0 is true. Move the slider to place the observed result farther from the center.

Observed distance1.0
Approximate two-sided p-value0.317
Decision at α = 0.05Do not reject H0

This visualization is intentionally generic. Chi-square and ANOVA do not use this exact bell-shaped null distribution; they have their own sampling distributions. Unlike this two-sided illustration, chi-square and F distributions are one-tailed: only unusually large values count as extreme, since both statistics are always non-negative. The important idea carries over regardless: more extreme results occupy less of the distribution under H0.

6. What is a p-value?

Definition at the level needed here
The p-value is the probability, assuming H0 is true, of obtaining a result at least as extreme as the one observed.

The phrase “at least as extreme” matters. We are not asking for the probability of obtaining exactly the same numerical result. We count the observed result and results that would be even more surprising under H0.

p-valueIntuition
LargeThe result is not especially unusual if H0 is true.
SmallThe result would be unusual if H0 were true.

7. What a small p-value tells us

Suppose an ANOVA test produces \(p = 0.003\). The correct interpretation at this level is:

Interpretation: If the null hypothesis of equal population means were true, a result at least this extreme would be quite unusual. Therefore, the data provide strong evidence against H0.

We do not say that H0 has a 0.3% probability of being true.

8. The significance level α

Before making the test decision, we choose a significance level, commonly written as \(\alpha\). A common choice is \(\alpha = 0.05\). This is the same \(\alpha\) from your intro statistics course: the Type I error rate — the chance of rejecting H0 when H0 is actually true.

ComparisonDecisionBasic meaning
\(p \le \alpha\)Reject H0The observed result is sufficiently unusual under H0.
\(p > \alpha\)Do not reject H0The evidence is not sufficiently strong to reject H0.
Very important: “Do not reject H0” does not mean “H0 has been proven true.” It means that the available evidence is not strong enough to reject H0 at the selected significance level.

9. Common p-value mistakes

Incorrect interpretationWhy it is wrongBetter interpretation
“p = 0.03 means there is a 3% chance H0 is true.” The p-value is calculated assuming H0, not by calculating the probability that H0 is true. Assuming H0 is true, results this extreme or more extreme would be rare.
“p = 0.03 means the result has 97% accuracy.” A p-value is not an accuracy measure. The p-value measures how unusual the data are under H0.
“p > 0.05 proves there is no relationship.” Failure to reject H0 is not proof that H0 is true. The data do not provide sufficiently strong evidence against H0.
“Smaller p-value always means a more important feature.” Statistical significance and practical / predictive importance are different ideas. Use the test to assess evidence against H0; use effect size or predictive evaluation for other questions.

10. Statistical significance is not the same as practical importance

With a very large sample, even a small difference can produce a very small p-value. With a small sample, a fairly large difference may fail to reach statistical significance.

Machine learning interpretation
A feature can be statistically associated with the target but still add little useful predictive information. Conversely, a potentially useful relationship may fail to reach statistical significance when the available sample is small. Therefore, a statistical filter is not a complete replacement for model-based evaluation.

11. A simple decision flow

StepQuestion
1What is H0?
2What would the data look like if H0 were true?
3How far is the observed result from that expectation?
4How often would a result this extreme occur under H0?
5Is the resulting p-value small enough to reject H0 at the chosen α?
Shortcut for remembering the logic
H0 → test statistic → p-value → decision

12. How this leads to chi-square feature selection

For a categorical feature and categorical target, the chi-square test asks whether the two variables are independent.

PartChi-square feature-selection interpretation
H0The feature and target are independent.
Observed dataThe counts in the contingency table.
Expected dataThe counts we would expect if the feature and target were independent.
Test statistic\(\chi^2\), measuring how far observed counts are from expected counts.
p-valueHow unusual this amount of departure would be if the variables were really independent.
Small p-valueEvidence against independence; the feature and target are associated.

13. How this leads to ANOVA feature selection

For a numerical feature and categorical target, ANOVA asks whether the group means are sufficiently different to provide evidence against the assumption that all population means are equal.

PartANOVA feature-selection interpretation
H0All population group means are equal.
Observed dataThe feature values within each target class.
Test statistic\(F\), comparing between-group variability with within-group variability.
p-valueHow unusual the observed separation among group means would be if the population means were really equal.
Small p-valueEvidence that at least one population mean differs from the others.
The common thread
Chi-square and ANOVA look different, but the hypothesis-testing logic is the same. Each defines H0, calculates a test statistic, and asks how unusual the observed statistic would be under H0.

14. Practice: Test Your Understanding

Answer all 5 questions. Click an option for instant feedback.

Your score: 0 / 5

15. Key Takeaways

  1. H0 is the default hypothesis that the test evaluates.
  2. A test statistic measures how far the observed data depart from what H0 predicts.
  3. A p-value asks how unusual the observed result, or something more extreme, would be if H0 were true.
  4. A small p-value gives evidence against H0; it is not the probability that H0 is true.
  5. At significance level \(\alpha\), reject H0 when \(p \le \alpha\).
  6. Statistical significance does not automatically mean practical or predictive importance.
  7. The same logic underlies both filters in Chapter 06: chi-square for categorical feature / categorical target relationships and ANOVA F-test for numerical feature / categorical target relationships.